#include "KeySearchEdit.h"

#include <core/StyleMgr.h>

#include <QKeyEvent>

#include "../KeySearchWidget.h"

KeySearchEdit::KeySearchEdit(QWidget* parent) : QLineEdit(parent) {
  setClearButtonEnabled(true);
  setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

  setStyleSheet("border:none;background:transparent;font:bold 12px");
  setFocusPolicy(Qt::FocusPolicy::ClickFocus);

  connect(this, &QLineEdit::textEdited, this, &KeySearchEdit::convertToUpper);
}

void KeySearchEdit::focusInEvent(QFocusEvent* event) {
  KeySearchWidget* ksw = dynamic_cast<KeySearchWidget*>(parent());
  if (ksw) ksw->foucsChanged(true);

  QString style =
      QString("border:none;background:transparent;color:#%1;font:bold 12px;")
          .arg(StyleMgr::instance().color("Highlight").rgb(), 0, 16);
  setStyleSheet(style);
  QLineEdit::focusInEvent(event);
}

void KeySearchEdit::focusOutEvent(QFocusEvent* event) {
  KeySearchWidget* ksw = dynamic_cast<KeySearchWidget*>(parent());
  if (ksw) ksw->foucsChanged(false);

  setStyleSheet("border:none;background:transparent;font:bold 12px");
  QLineEdit::focusOutEvent(event);
}

void KeySearchEdit::convertToUpper(const QString& text) {
  setText(text.toUpper());
}
